package com.shierke.Ume.util

import android.content.Context
import android.content.SharedPreferences
import android.content.pm.PackageManager
import android.content.res.Resources
import android.text.TextUtils
import android.view.ViewGroup
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.core.app.ActivityCompat
import java.text.SimpleDateFormat
import java.util.*


inline val Context.sharedPreferences: SharedPreferences
    get() = getSharedPreferences("sp_file", Context.MODE_PRIVATE)

fun Context.res2color(@ColorRes colorResId: Int): Int = resources.getColor(colorResId)

fun Context.res2String(@StringRes id: Int): String = resources.getString(id)

fun Context.px2dp(pxValue: Float): Int {
    val scale = resources.displayMetrics.density
    return (pxValue / scale + 0.5f).toInt()
}

fun Context.dp2px(dipValue: Float): Int {
    val scale = resources.displayMetrics.density
    return (dipValue * scale + 0.5f).toInt()
}


val screenWidth: Int = Resources.getSystem().displayMetrics.widthPixels
val screenHeight: Int = Resources.getSystem().displayMetrics.heightPixels


inline fun Context.grantPermission(permission: String): Boolean {
    return PackageManager.PERMISSION_GRANTED == ActivityCompat.checkSelfPermission(this, permission)
}

val Context.config: BaseConfig get() = BaseConfig.instance()

fun Context.getSharedPrefs() = getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE)

inline fun formateDate(millin: Long, formate: String): String {
    if (millin == 0L) {
        return ""
    }

    if (TextUtils.isEmpty(formate)) {
        throw IllegalArgumentException("formagt can not be null or empty")
    }
    val sdf = SimpleDateFormat(formate)
    val date = Date(millin)
    return sdf.format(date)
}

fun Map<String, Any?>.mapValuesToString(): Map<String, String> {
    return filter { it.value != null }
        .map { it.key to it.value.toString() }
        .toMap()
}



